﻿using System;
using UnityEngine;

public class MoveAloneSplineOnEnable : MonoBehaviour
{
    public float duration = 1f;
    public bool loop = true;
    private Coroutine playing;
    public BezierSpline spline;
    public Transform target;

    private void OnEnable()
    {
        this.Play();
    }

    private void Play()
    {
        if (this.playing == null)
        {
            this.playing = this.StartAnimateFloat(0f, 1f, this.duration, delegate (float x) {
                if (this.target != null)
                {
                    this.target.localPosition = this.spline.Evaluate(x);
                    this.target.localRotation *= Quaternion.FromToRotation(this.target.right, this.spline.EvaluateNormal(x));
                }
            }, delegate {
                this.playing = null;
                if (this.loop)
                {
                    this.Play();
                }
            });
        }
    }
}

